//
//  LXDownloadTableViewCell.m
//  cat
//
//  Created by imera on 16/3/26.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXDownloadTableViewCell.h"

@interface LXDownloadTableViewCell()

/** 视频缩略图 */
@property (nonatomic, weak) UIImageView *videoThumb;

/** 视频名称 */
@property (nonatomic, weak) UILabel *videoName;

/** 缓存进度 */
@property (nonatomic, weak) UILabel *downloadProgress;

/** 是否选中 */
@property (nonatomic, weak) UIImageView *selectedImageView;

@end

@implementation LXDownloadTableViewCell

- (void)setIsEdit:(BOOL)isEdit
{
    _isEdit = isEdit;
    self.selectedImageView.hidden = !isEdit;
}

- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    if (isSelected) {
        self.selectedImageView.image = [UIImage imageNamed:@"selected"];
    }else{
        self.selectedImageView.image = [UIImage imageNamed:@"select"];
    }
}

- (void)setDict:(NSDictionary *)dict
{
    if (dict != nil) {
        _dict = dict;
    }
    [self layoutSubviews];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubviews];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubviews];
        self.backgroundColor = MainBackgroundColor;
    }
    return self;
}

- (void)initSubviews
{
    UIImageView *videoThumb = [[UIImageView alloc]init];
    [self addSubview:videoThumb];
    self.videoThumb = videoThumb;
    
    UILabel *videoName = [[UILabel alloc]init];
    videoName.font = [UIFont systemFontOfSize:13];
    [self addSubview:videoName];
    self.videoName = videoName;
    
    UILabel *downloadProgress = [[UILabel alloc]init];
    downloadProgress.font = [UIFont systemFontOfSize:13];
    [self addSubview:downloadProgress];
    self.downloadProgress = downloadProgress;
    
    UIImageView *selectedImageView = [[UIImageView alloc]init];
    selectedImageView.image = [UIImage imageNamed:@"select"];
    selectedImageView.hidden = YES;
    self.selectedImageView = selectedImageView;
    [self addSubview:selectedImageView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    /** 视频缩略图 */
    UIImageView *videoThumb = self.videoThumb;
    if (self.dict != nil) {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        videoThumb.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.jpg", path , self.dict[@"videoPicture"]]];
    }

    CGFloat videoThumbX = 10;
    CGFloat videoThumbY = 10;
    CGFloat videoThumbW = self.frame.size.width * 0.3;
    CGFloat videoThumbH = self.frame.size.height - videoThumbY * 2;
    videoThumb.frame = CGRectMake(videoThumbX, videoThumbY, videoThumbW, videoThumbH);

    /** 视频名称 */
    UILabel *videoName = self.videoName;
    if (self.dict != nil) {
        videoName.text = self.dict[@"videoName"];
    }
    
    CGSize videoNameSzie = [videoName.text sizeWithAttributes:@{NSFontAttributeName:videoName.font}];
    CGFloat videoNameX = CGRectGetMaxX(videoThumb.frame) + 20;
    CGFloat videoNameY = videoThumbY;
    CGFloat videoNameW = self.frame.size.width - videoThumbW - 30;
    videoName.numberOfLines = (videoNameSzie.width / videoNameW) + 1;
    CGFloat videoNameH = videoNameSzie.height * videoName.numberOfLines;
    videoName.frame = CGRectMake(videoNameX, videoNameY, videoNameW, videoNameH);
    
    /** 缓存进度 */
    UILabel *downloadProgress = self.downloadProgress;
    if ([self.dict[@"videoProgress"] isEqualToString:@"1.000"]) {
        downloadProgress.text = [NSString stringWithFormat:@"缓存完成"];
    }else{
        downloadProgress.text = [NSString stringWithFormat:@"缓存进度:%.1f%@",([self.dict[@"videoProgress"] doubleValue] * 100),@"%"];
    }
    CGSize downloadProgressSzie = [videoName.text sizeWithAttributes:@{NSFontAttributeName:videoName.font}];
    CGFloat downloadProgressX = videoNameX;
    CGFloat downloadProgressW = downloadProgressSzie.width;
    CGFloat downloadProgressH = downloadProgressSzie.height;
    CGFloat downloadProgressY = CGRectGetMaxY(videoThumb.frame) - downloadProgressH;
    downloadProgress.frame = CGRectMake(downloadProgressX, downloadProgressY, downloadProgressW, downloadProgressH);
    
    /** 勾选框 */
    UIImageView *selectedImageView = self.selectedImageView;

    CGFloat selectedImageViewW = 20;
    CGFloat selectedImageViewH = 20;
    CGFloat selectedImageViewX = self.frame.size.width - selectedImageViewW - 10;
    CGFloat selectedImageViewY = (self.frame.size.height - selectedImageViewH) / 2;
    selectedImageView.frame = CGRectMake(selectedImageViewX, selectedImageViewY, selectedImageViewW, selectedImageViewH);
}
@end
